//
//  HDCookieManager.swift
//  Excited
//
//  Created by X140Yu on 7/30/16.
//  Copyright © 2016 X140Yu. All rights reserved.
//

import Foundation
import YYKit

enum HDCookieType: String {
    case B = "CookieB"
    case D = "CookieD"
    case E = "CookieE"
}

internal struct Key {
    static let User = "HDCookieManager"
}

class HDCookieManager {
    
    static let sharedInstance = HDCookieManager()
    
    private let diskCache: YYDiskCache!
    
    private init() {
        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
        diskCache = YYDiskCache(path: path)
    }
    
    
    // MARK: Cookie B
    func getSecondCookie(cookie: String) -> String? {
        let a = cookie.characters.split{$0 == ","}.map(String.init)
        if a.count == 2 {
            return a[1].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
        } else {
            return nil;
        }
    }
    
    func saveCookie(cookie: String, type: HDCookieType) {
        diskCache.setObject(cookie, forKey: type.rawValue)
    }
    
    func getCookie(type: HDCookieType) -> String? {
        let cookieB = diskCache.objectForKey(type.rawValue)
        return cookieB as? String
    }
}